<template>
  <div>
    <div v-if="shows == 'list'">
      <Header>
        <div>
          <el-button size="small" type="primary" @click="addCoupon"
            >新建优惠劵</el-button
          >
        </div>
      </Header>

      <Main :data="listData" :footer-lock="totals > 10">
        <el-table-column
          label="优惠劵名称"
          align="center"
          property="title"
        ></el-table-column>
        <el-table-column label="优惠劵类型" align="center">
          <template slot-scope="scope">
            {{ scope.row.type == 0 ? "满减劵" : "折扣劵" }}
          </template>
        </el-table-column>
        <el-table-column label="有效期" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type_time === 1">
              {{ scope.row.stime }} 至 {{ scope.row.etime }}</span
            >
            <span v-else
              >领取{{ scope.row.stime }}天后生效，{{
                scope.row.etime === 0
                  ? "永久有效"
                  : `有效期${scope.row.etime}天`
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
          property="status"
        ></el-table-column>
        <el-table-column label="库存" align="center">
          <template slot-scope="scope">
            {{ scope.row.stock }}&nbsp;&nbsp;
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="editStock(scope.row)"
            ></el-button>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          class-name="tableoperation"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="msg(scope.row.lg_coupon_id)"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="del(scope.row.lg_coupon_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </Main>
      <Footer :total="totals">
        <el-pagination
          :page-size="10"
          layout="prev, pager, next"
          :total="totals"
          @current-change="changes"
        >
        </el-pagination>
      </Footer>
    </div>

    <coupon-add v-if="shows == 'add'" :data-msg="dataMsg" @out="out" />

    <coupon-msg v-if="shows == 'msg'" :lg-coupon-id="lgCouponId" @out="out" />

    <el-dialog title="修改库存" :visible.sync="dialogFormVisible" width="28%">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="当前库存">
          <el-input-number
            v-model="form.stock"
            :controls="false"
            disabled
            size="medium"
          ></el-input-number
          >&nbsp;&nbsp;个
        </el-form-item>
        <el-form-item label="追加库存" prop="addStock">
          <el-input-number
            v-model="form.addStock"
            :step="1"
            :precision="0"
            :controls="false"
            size="medium"
            placeholder="请输入"
          ></el-input-number
          >&nbsp;&nbsp;个
          <div style="height: 18px;color:#909399;font-size:12px;">
            输入正整数为增加，负数为减少，仅可输入整数
          </div>
        </el-form-item>
        <el-form-item label="修改后库存">
          <el-input-number
            v-model="allStock"
            :controls="false"
            disabled
            size="medium"
          ></el-input-number
          >&nbsp;&nbsp;个
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="stockSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { COUPON_LIST, DEL_COUPON, ADD_STOCK } from "../service";

export default {
  components: {
    couponAdd: () => import("./form"),
    couponMsg: () => import("./msg")
  },
  data() {
    return {
      listData: [],
      shows: "list",
      totals: 0,
      pages: 1,
      lgCouponId: "",
      st_lg_coupon_id: "",
      dataMsg: {},
      dialogFormVisible: false,
      form: {
        stock: 0,
        addStock: 0
      },
      rules: {
        addStock: [{ required: true, trigger: "blur" }]
      }
    };
  },
  computed: {
    allStock: {
      get() {
        return this.form.stock + this.form.addStock;
      },
      set() {}
    }
  },
  created() {
    this.getList();
  },
  methods: {
    editStock(e) {
      this.form.stock = e.stock;
      this.form.addStock = 0;
      this.st_lg_coupon_id = e.lgCouponId;
      this.dialogFormVisible = true;
    },
    async stockSubmit() {
      await this.$refs["form"].validate(valid => {
        if (valid) {
          ADD_STOCK({
            stock: this.allStock,
            lg_coupon_id: this.st_lg_coupon_id
          }).then(({ code, msg }) => {
            if (code === 200) {
              this.getList();
              this.dialogFormVisible = false;
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        } else {
          return false;
        }
      });
    },
    msg(lgCouponId) {
      this.shows = "msg";
      this.lgCouponId = lgCouponId;
    },
    async del(lgCouponId) {
      await this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DEL_COUPON({ lg_coupon_id: lgCouponId }).then(({ code, msg }) => {
            if (code === 200) {
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除!"
          });
        });
    },
    addCoupon() {
      this.shows = "add";
      this.dataMsg = {};
    },
    out(statu, data) {
      switch (statu) {
        case "edit":
          this.shows = "add";
          this.dataMsg = data;
          break;
        case "msg":
          this.shows = "msg";
          this.lgCouponId = data;
          break;
        case "list":
          this.shows = "list";
          this.getList();
          break;
      }
    },
    changes(page) {
      this.pages = page;
      this.getList();
    },
    async getList() {
      let prams = {
        page: this.pages,
        pageshow: 10
      };
      await COUPON_LIST(prams).then(({ code, data, msg }) => {
        if (code === 200) {
          this.totals = data.count;
          this.listData = data.list;
        } else {
          this.$message.error(msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
